﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Dazaza.Framework
{
    public class Strings
    {
        /// <summary>
        /// 将字符转为sql安全字符。
        /// </summary>
        /// <param name="str">将要转换的字符</param>
        /// <param name="IsDel">发现不安全的字符是删除还是替换，true则去除不安全的字符，false则用相应的字符替换不安全的字符。如：把单引号替换成＆#39;</param>
        /// <returns></returns>
        public static string SqlSafeString(string str, bool IsDel)
        {
            if (str != null && str != "")
            {
                if (IsDel)
                {
                    str = str.Replace("'", "");
                    str = str.Replace("\"", "");
                }
                else
                {
                    str = str.Replace("'", "&#39;");
                    str = str.Replace("\"", "&#34;");
                }
            }
            return str;
        }

        /// <summary>
        /// 分割字符串,返回分割后的数组
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="splitstr">分隔符</param>
        /// <returns></returns>
        public static string[] SplitMulti(string str, string splitstr)
        {
            string[] strarr = null;
            if (str != null && str != "")
            {
                Regex regex1 = new Regex(splitstr);
                strarr = regex1.Split(str);
            }
            return strarr;
        }

        /// <summary>
        /// 检查文本中是否全部是英文字符
        /// </summary>
        /// <param name="str">要检查的文本</param>
        /// <returns></returns>
        public static bool IsStringChinese(string str)
        {
            bool b = false;
            str = str.Trim();
            if (str.Length != StringLength(str))
            {
                b = true;
            }
            return b;
        }

        /// <summary>
        /// 计算文本长度，一个中文算2个，一个英文算1个
        /// </summary>
        /// <param name="str">要的计算文本</param>
        /// <returns></returns>
        public static int StringLength(string str)
        {
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(str.Trim());
            return mybyte.Length;
        }     

        /// <summary>
        /// 按字符截字
        /// </summary>
        /// <param name="s"></param>
        /// <param name="length"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public static string Substring(string s, int length, string suffix)
        {
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
            int n = 0;  //  表示当前的字节数
            int i = 0;  //  要截取的字节数
            for (; i < bytes.GetLength(0) && n < length; i++)
            {
                //  偶数位置，如0、2、4等，为UCS2编码中两个字节的第一个字节
                if (i % 2 == 0)
                    n++;      //  在UCS2第一个字节时n加1
                else
                {
                    //  当UCS2编码的第二个字节大于0时，该UCS2字符为汉字，一个汉字算两个字节
                    if (bytes[i] > 0)
                        n++;
                }
            }
            //  如果i为奇数时，处理成偶数
            if (i % 2 == 1)
            {
                //  该UCS2字符是汉字时，去掉这个截一半的汉字
                if (bytes[i] > 0)
                    i = i - 1;
                //  该UCS2字符是字母或数字，则保留该字符
                else
                    i = i + 1;
            }
            string result = System.Text.Encoding.Unicode.GetString(bytes, 0, i);
            if (!string.IsNullOrEmpty(result) && bytes.GetLength(0) > length)
                return result + suffix;
            return result;
        }

        public static string Substring(string s, int length)
        {
            return Substring(s, length, "");
        }

        /// <summary>
        /// 过滤重要信息
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string HideImportantStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str))
                str = "";
            if (str.Length <= len)
                return str;
            string result = str.Substring(0, len);
            if (str.Length == 11 && isNumber(str))
            {
                result = str.Substring(0, 6) + "***";
            }
            else if (str.IndexOf('@') != -1)
            {
                str = str.Split('@')[0];
                if (str.Length > len)
                {
                    result = str.Substring(0, len);
                    result += "***";
                }
                else
                {
                    result = str;
                }
                result += "@***";
            }
            else
            {

                result += "***";
            }
            return result;
        }

        public static bool isNumber(string str)
        {
            Regex numReg = new Regex(@"^\d+$");
            if (numReg.IsMatch(str))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 过滤html，只返回文本
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetStringWithOutHtml(string s)
        {
            return GetStringWithOutHtml(s, true);
        }

        public static string GetStringWithOutHtml(string s, bool isRemoveUnVisibileChar)
        {
            return GetStringWithOutHtml(s, isRemoveUnVisibileChar, true);
        }

        public static string GetStringWithOutHtml(string s, bool isRemoveUnVisibileChar, bool isRemoveHtmlSpace)
        {
            s = Regex.Replace(s, "<.*?>", "");
            if(isRemoveUnVisibileChar)
                s = Regex.Replace(s, @"[\s]", "");
            if (isRemoveHtmlSpace)
            {
                s = Regex.Replace(s, @"&(quot|#34);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(amp|#38);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(lt|#60);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(gt|#62);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
            }
            return s;
        }
        public static string ConvertEncoding(string str, Encoding scr, Encoding dst)
        {
            byte[] bytes = scr.GetBytes(str);
            byte[] asciiBytes = Encoding.Convert(scr, dst, bytes);
            char[] asciiChars = new char[dst.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            dst.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            string info = new string(asciiChars);
            return info;
        }

        public static string ReplaceSpecial(string str)
        {
            str = str.Replace("&", " ");
            str = str.Replace("#", " ");
            str = str.Replace("^", " ");
            str = str.Replace("*", " ");
            str = str.Replace("+", " ");
            str = str.Replace("-", " ");
            str = str.Replace("%", " ");
            str = str.Replace("$", " ");
            str = str.Replace("@", " ");
            str = str.Replace("\\", " ");
            str = str.Replace("//", " ");
            str = str.Replace("//", " ");
            str = str.Replace(":", " ");
            str = str.Replace("\"", " ");
            str = str.Replace("/", " ");
            str = str.Replace("{", " ");
            str = str.Replace("}", " ");
            str = str.Replace("[", " ");
            str = str.Replace("]", " ");
            str = str.Replace("(", " ");
            str = str.Replace(")", " ");
            return str;
        }
    }
}
